from django.core.paginator import Paginator
from django.shortcuts import render

from PureLand.models import Article


# client = connect() #返回一个客户端连接
# db = client['jianshu'] #返回一个数据库
# collection = db['articles'] #返回一个集合


def login(request):
    pass


def register(request):
    pass


def home(request):
    context = {'content': 'home'}
    return render(request, 'home.html', context)


def article(request):
    article_info = Article.objects
    paginator = Paginator(article_info, per_page=10)
    page_num = request.GET.get('page', default=1)

    loaded = paginator.page(page_num)
    context = {
        'articles': loaded
    }
    return render(request, 'article.html', context)


def search(request):
    """
    查找文章
    :param request:
    :return:
    """
    key = request.GET['search_key']
    res = Article.objects(content__contains=key)
    paginator = Paginator(res, per_page=10)
    page_num = request.GET.get('page', default=1)

    loaded = paginator.page(page_num)
    context = {'articles': loaded, 'key': key}
    return render(request, 'search.html', context)


def article_detail(request):
    pass
